package cn.gov.pbc.tims.util;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.gov.pbc.tims.entity.User;

@Component("operator")
public class Operator {

	@Autowired
	private HttpSession httpSession;
	@Autowired
	private SessionFactory sessionFactory;
	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	public boolean isLogin(){		
		User op=(User)httpSession.getAttribute("op");
		return op!=null;
	}
	public User authenticate(String usercode,String password){
		String hql="from User u where u.code=:code and u.password=:password";
		List<User> Users = this.getCurrentSession().createQuery(hql,User.class)
				.setParameter("code",usercode).setParameter("password", password).getResultList();
		if(Users.size()==1){
			User user=Users.get(0);
			httpSession.setAttribute("op", user);
			return user;
		}
		httpSession.removeAttribute("op");
		return null;
	}
	public void setUser(User user)
	{
		httpSession.setAttribute("op", user);
	}
	public User getUser(){
		User op=(User)httpSession.getAttribute("op");
		return op;
	}
	public void logout(){
		httpSession.removeAttribute("op");
	}
	/*2——'系统管理员'
	1—— '人民银行操作员'
	0—— '金融机构操作员'*/
	public String getPower(){
		String power="";
		User op=getUser();
		/*switch (op.getRoleId()) {
		case "001":
			power = 1;
			break;
		case "002":
			power = 2;
			break;
		case "003":
			power = 3;
			break;
		default:
			break;
		}*/
		return op.getRoleId();
	}
}
